function [ output_args ] = plsr2(ncomp)

[file_name, file_path] = uigetfile('../stats/*.csv', 'Load Wrist Data', 'MultiSelect', 'on');

store_path = fullfile(file_path, '..', 'plsr');
pctvar_path = fullfile(file_path, '..', 'pctvar');
residual_path = fullfile(file_path, '..', 'residual');
mse_path = fullfile(file_path, '..', 'mse');

if ~exist(store_path, 'dir')
    mkdir(store_path);
end

if ~exist(pctvar_path, 'dir')
    mkdir(pctvar_path);
end

if ~exist(residual_path, 'dir')
    mkdir(residual_path);
end

if ~exist(mse_path, 'dir')
    mkdir(mse_path);
end

if ~isequal(file_name, 0)
    file_name = cellstr(file_name);  % Care for a single selection
    for i = 1:numel(file_name)
        wrist_data =  csvread(fullfile(file_path, file_name{i}));
        for j =1:20:size(wrist_data,2)
            clf('reset');
            
            time_step = wrist_data(:,j);
            weight = wrist_data(:,j+1);
            mu = wrist_data(:,j+2);
            force = wrist_data(:,j+3:j+5);
            torque = wrist_data(:,j+6:j+8);
            pos = wrist_data(:,j+9:j+11);
            quat = wrist_data(:,j+12:j+15);
            status = wrist_data(:,j+17);
            con_force = wrist_data(:, j+18:j+20);

            X = [force torque pos quat status con_force];
            Y = [weight mu];
            
            X = zscore(X);
            %Y = zscore(Y);
            
            [XL, YL, XS, YS, BETA, PCTVAR, MSE, STATS] = plsregress(X, Y, ncomp);
            
            %save PCTVAR
            set(0,'DefaultFigureVisible', 'off');
            plot(1:ncomp,cumsum(100*PCTVAR(2,:)),'-bo');
            xlabel('Number of PLS components');
            ylabel('Percent Variance Explained in y');          
            csvwrite(fullfile(store_path, [num2str((j-1)/20 + 1) '.csv']), BETA);
            saveas(gcf,fullfile(pctvar_path, [num2str((j-1)/20 + 1) '.png']));
            
            %save MSE
            set(0,'DefaultFigureVisible', 'off');
            plot(0:ncomp,MSE(2,:),'-bo');
            xlabel('Number of PLS components');
            ylabel('Estimated Mean Squared Prediction Error');
            saveas(gcf,fullfile(mse_path, [num2str((j-1)/20 + 1) '.png']));

            %save y residual
            set(0,'DefaultFigureVisible', 'off');
            subplot(2,1,1);
            plot(1:size(STATS.Yresiduals,1),STATS.Yresiduals(:,1),'o');
            xlabel('Observation M');
            ylabel('Residual M');
            subplot(2,1,2);
            plot(1:size(STATS.Yresiduals,1),STATS.Yresiduals(:,2),'o');
            xlabel('Observation MU');
            ylabel('Residual MU');
            saveas(gcf,fullfile(residual_path, [num2str((j-1)/20 + 1) '.png']));
        end
    end
end

end

